﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NET.Tools.OS;
using System.IO;
using System.Resources;
using ResolutionManager.Properties;
using System.Collections;

namespace ResolutionManager
{
    public class UpdateContextMenuHandlerEventArgs : EventArgs
    {
        public ICollection Resolution2DList { get; private set; }
        public ICollection Resolution3DList { get; private set; }

        public UpdateContextMenuHandlerEventArgs(ICollection resolution2D, ICollection resolution3D)
        {
            Resolution2DList = resolution2D;
            Resolution3DList = resolution3D;
        }
    }

    public delegate void UpdateContextMenuHandler(object sender, UpdateContextMenuHandlerEventArgs e);

    public partial class ConfigurationWindow : Form
    {
        /// <summary>
        /// Handle events for updating the context menu of notify icon
        /// </summary>
        public event UpdateContextMenuHandler UpdateContextMenu;

        public ConfigurationWindow()
        {
            InitializeComponent();
            Initialize();
            this.Icon = Icon.FromHandle(Resources.icon.GetHicon());
        }

        private void Initialize()
        {
            lstNormal.Items.Clear();
            lst3D.Items.Clear();

            List<DisplayScreen> resolutions = NET.Tools.OS.ResolutionManager.ResolutionList;
            List<ResolutionData> savedResolutions = IO.Load();
            foreach (DisplayScreen resolution in resolutions)
            {
                if (find(savedResolutions, resolution))
                {
                    lst3D.Items.Add(resolution);
                }
                else
                {
                    lstNormal.Items.Add(resolution);
                }
            }
        }

        private bool find(List<ResolutionData> lst, DisplayScreen screen)
        {
            foreach (ResolutionData data in lst)
            {
                if (data.Equals(screen))
                {
                    return true;
                }
            }

            return false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lstNormal_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnTo3D.Enabled = lstNormal.SelectedIndex > -1;
        }

        private void lst3D_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnFrom3D.Enabled = lst3D.SelectedIndex > -1;
        }

        private void btnTo3D_Click(object sender, EventArgs e)
        {
            lst3D.Items.Add(lstNormal.SelectedItem);
            lstNormal.Items.Remove(lstNormal.SelectedItem);
        }

        private void btnFrom3D_Click(object sender, EventArgs e)
        {
            lstNormal.Items.Add(lst3D.SelectedItem);
            lst3D.Items.Remove(lst3D.SelectedItem);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            IO.Save(lst3D.Items);
            UpdateContextMenu(this, new UpdateContextMenuHandlerEventArgs(lstNormal.Items, lst3D.Items));
            Close();
        }
    }
}
